<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="通知标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入通知标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item style="margin-left: 50px" label="发布时间" prop="publishTime">
        <el-date-picker
          v-model="queryParams.timeQuantum"
          type="daterange"
          align="center"
          unlink-panels
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['patrol:notification:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="notificationList" style="color: #333333"
              :header-cell-style="{background:'#E3E3E3'}">

      <el-table-column label="发布日期" prop="publishTime" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通知标题" align="center" prop="title"/>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status==='0'">
            <span style="color: #1E8BFC">{{ scope.row.readYes }}已读</span> /
            <span style="color: #DA1E1E">{{ scope.row.readNo }}未读</span>
          </div>
          <span v-else-if="scope.row.status==='1'" style="color: #1E8BFC">待编辑</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div v-if="scope.row.status==='0'">
            <el-button
              size="mini"
              type="text"
              @click="handleDetail(scope.row)"
            >详情
            </el-button>
            <span style="color: #2C8DEB"> &emsp;| </span>
            <el-button
              :disabled="scope.row.readYes"
              :style="scope.row.readYes===0?'':'color: #979797'"
              size="mini"
              type="text"
              @click="handleRecall(scope.row)"
              v-hasPermi="['patrol:notification:edit']"
            >撤回
            </el-button>
            <span style="color: #2C8DEB"> | </span>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['patrol:notification:remove']"
            >删除
            </el-button>
          </div>
          <div v-if="scope.row.status==='1'">
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(scope.row)"
            >编辑
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {
  listNotification,
  delNotification,
  recallNotification
} from "@/api/patrol/notification";
import {getReadNum} from "@/api/patrol/notifination_user";

export default {
  name: "Notification",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通知公告表格数据
      notificationList: [],
      // 弹出层标题
      title: "",
      // 是否从首页代办事项进入
      isOuter: false,
      // 从首页代办事项进入时传入的标题
      titleOuter: "",
      // 是否显示弹出层
      open: false,
      // 是否回复字典
      answerTypeOptions: [],
      // 通知状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        startTime: null,
        finishTime: null,
        //选择的时间段
        timeQuantum: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "通知标题不能为空", trigger: "blur"}
        ],
        content: [
          {required: true, message: "通知内容不能为空", trigger: "blur"}
        ],
        answerType: [
          {required: true, message: "是否回复不能为空", trigger: "change"}
        ],
      },
      //时间范围选择
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      }
    };
  },
  created() {
    // 如果不是从首页进入，则执行，如果是首页进入，执行mounted中的查询
    this.isOuter = this.$route.query.isOuter;
    if (!this.isOuter) {
      this.getList();
    }
    this.getDicts("answer_type").then(response => {
      this.answerTypeOptions = response.data;
    });
    this.getDicts("sys_notice_status").then(response => {
      this.statusOptions = response.data;
    });
    this.titleOuter = this.$route.query.title;
  },
  mounted() {
    if (this.isOuter) {
      this.isOuter = false;
      this.queryParams.title = this.titleOuter;
      this.handleQuery();
    }
  },
  methods: {
    /** 查询通知公告列表 */
    getList() {
      this.loading = true;
      this.queryParams.startTime = this.queryParams.timeQuantum[0]
      this.queryParams.finishTime = this.queryParams.timeQuantum[1]
      let data = JSON.parse(JSON.stringify(this.queryParams))
      this.$delete(data, 'timeQuantum')
      listNotification(data).then(response => {
        this.notificationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.timeQuantum = ''
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: "/notification/add",
        query: {
          notifyId: "false"
        }
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.$router.push({
        name: 'NotifyDetail',
        path: "/notification/detail",
        query: {
          id: encodeURI(JSON.stringify(row.id))
        }
      });
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      this.$tab.openPage("编辑通知", "/notification/add", {notifyId: row.id});
    },
    /** 撤回按钮操作 */
    handleRecall(row) {
      const ids = row.id || this.ids;
      const title = row.title;
      this.$confirm('是否确认撤回通知"' + title + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      }).then(() => {
        getReadNum({'notificationId': row.id})
          .then(res => {
            if (res > 0) {
              this.msgError("用户已读，不允许撤回！")
            } else {
              recallNotification(ids).then(() => {
                this.getList();
                this.msgSuccess("撤回成功");
              })
            }
          })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const title = row.title;
      this.$confirm('是否确认删除通知"' + title + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      }).then(function () {
        return delNotification(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    }
  }
};

</script>
<style scoped>

</style>
